home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Workbench Add-On
/
Workbench Add-On - Volume 1.iso
/
BBS-Archive
/
Dev
/
GNU-SMALLTALK.lha
/
String.st
< prev
next >
Wrap
Text File
|
1992-03-01
|
7KB
|
292 lines
"======================================================================
|
| String Method Definitions
|
======================================================================"
"======================================================================
|
| Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
| Written by Steve Byrne.
|
| This file is part of GNU Smalltalk.
|
| GNU Smalltalk is free software; you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by the Free
| Software Foundation; either version 1, or (at your option) any later version.
|
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
| FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
| details.
|
| You should have received a copy of the GNU General Public License along with
| GNU Smalltalk; see the file COPYING. If not, write to the Free Software
| Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
======================================================================"
"
| Change Log
| ============================================================================
| Author Date Change
| sbb 23 Feb 92 Added asInteger and asByteArray.
|
| sbb 16 Mar 91 Class creation now separate statement.
|
| sbb 4 Feb 91 Fixed duplicate fileName definition to be filePos
|
| sbb 21 Sep 90 Changed printOn: to print with quote marks. On
| re-reading the documentation it appears as if this is
| the proper behavior (sigh).
|
| sbyrne 3 Sep 89 added asString for method source package
|
| sbyrne 25 Apr 89 created.
|
"
ArrayedCollection variableByteSubclass: #String
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: nil
!
String comment:
'My instances represent string data types. I provide accessing and
manipulation methods for string data types.' !
!String class methodsFor: 'basic'!
fromString: aString
" ### probably should use regular copy"
^aString copyFrom: 1 to: aString size
!
readFrom: aStream
| str newChar |
str _ WriteStream on: (String new: 0).
aStream do: " ### Is this risky, doing a next inside? "
[ :char | char == $'
ifTrue: [ newChar _ aStream next.
newChar == $' ifFalse: [ str nextPut: $' ].
str nextPut: newChar ]
ifFalse: [ str nextPut: char ]
].
^str contents
!!
!String methodsFor: 'comparing'!
< aString
"Return true if the receiver is less than aString, ignoring case
differences."
self >= aString ifTrue: [ ^false ]
ifFalse: [ ^true ]
!
> aString
"Return true if the receiver is greater than aString, ignoring case
differences."
self <= aString ifTrue: [ ^false ]
ifFalse: [ ^true ]
!
<= aString
"Returns true if the receiver is less than or equal to aString,
ignoring case differences. If is receiver is an initial substring of
aString, it is considered to be less than aString."
| c1 c2 |
" Scan self and aString until a character is clearly greater or lesser
(All preceding characters must have been equal). If the end is reached,
one of the strings is a possibly improper initial substring of the other,
and for the receiver to be less than aString, it must be the initial
substring."
1 to: (self size min: aString size) do:
[ :i | c1 _ (self at: i) asLowercase.
c2 _ (aString at: i) asLowercase.
c1 < c2 ifTrue: [ ^true ].
c1 > c2 ifTrue: [ ^false ] ].
^self size <= aString size
!
>= aString
"Returns true if the receiver is greater than or equal to aString,
ignoring case differences. If is aString is an initial substring of
the receiver, it is considered to be less than the receiver."
| c1 c2 |
1 to: (self size min: aString size) do:
[ :i | c1 _ (self at: i) asLowercase.
c2 _ (aString at: i) asLowercase.
c1 < c2 ifTrue: [ ^false ].
c1 > c2 ifTrue: [ ^true ] ].
^self size >= aString size
!
sameAs: aString
"Returns true if the receiver is the same string as aString, ignoring
case differences."
self size ~= aString size ifTrue: [ ^false ].
1 to: self size do:
[ :i | (self at: i) asLowercase ~= (aString at: i) asLowercase
ifTrue: [ ^false ] ].
^true
!
match: aString
^self asLowercase matchSubstring: 1 in: aString asLowercase at: 1
!!
!String methodsFor: 'converting'!
asUppercase
"Returns a copy of self as an uppercase string"
| newStr |
newStr _ self species new: self size.
1 to: self size do:
[ :i | newStr at: i put: (self at: i) asUppercase ].
^newStr
!
asLowercase
"Returns a copy of self as a lowercase string"
| newStr |
newStr _ self species new: self size.
1 to: self size do:
[ :i | newStr at: i put: (self at: i) asLowercase ].
^newStr
!
asString
"But I already am a string! Really!"
^self
!
asByteArray
| byteArray size |
size _ self size.
byteArray _ ByteArray new: size.
byteArray replaceFrom: 1 to: size withString: self startingAt: 1.
^byteArray
!
asInteger
| result |
result _ 0.
self do:
[ :ch | ch isDigit
ifFalse: [ ^result ].
result _ result * 10 + ch digitValue ].
^result
!
fileName
"But I don't HAVE a file name!"
^nil
!
filePos
"But I don't HAVE a file position!"
^nil
!
asSymbol
"Returns the symbol corresponding to the string"
^Symbol intern: self
!!
!String methodsFor: 'copying'!
shallowCopy
| newStr |
newStr _ self species new: self size.
1 to: self size do:
[ :i | newStr at: i put: (self at: i) ].
^newStr
!
deepCopy
^self shallowCopy
!
, aString
| newString mySize |
newString _ self species new: ((mySize _ self size) + aString size).
newString primReplaceFrom: 1 to: mySize with: self startingAt: 1.
newString primReplaceFrom: mySize + 1 to: newString size
with: aString startingAt: 1.
^newString
!
replaceFrom: start to: stop with: replacementCollection
self replaceFrom: start to: stop with: replacementCollection
startingAt: 1
!
replaceFrom: start to: stop with: replacementCollection startingAt: repStart
self primReplaceFrom: start to: stop with: replacementCollection
startingAt: repStart
!!
!String methodsFor: 'printing'!
printOn: aStream
self storeOn: aStream
!!
!String methodsFor: 'storing'!
storeOn: aStream
aStream nextPut: $'.
self do:
[ :char | char == $' ifTrue: [ aStream nextPut: char ].
aStream nextPut: char ].
aStream nextPut: $'
!!
!String methodsFor: 'private'!
matchSubstring: p in: aString at: s
| pc |
p > self size
ifTrue: [ ^s > aString size ].
pc _ self at: p.
pc = $*
ifTrue: [ s to: (aString size) + 1 do:
[ :ss | (self matchSubstring: p + 1
in: aString
at: ss)
ifTrue: [ ^true ] ].
^false ].
s > aString size ifTrue: [ ^false ].
pc = $#
ifTrue: [ ^self matchSubstring: p + 1 in: aString at: s + 1 ].
pc = (aString at: s)
ifTrue: [ ^self matchSubstring: p + 1 in: aString at: s + 1 ].
^false
!!